#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=1020;
const int INF=0x3f3f3f3f;
double m[MAXN][MAXN];
int n,q,s,t;
void floyd(){
    for(int k=0;k<n;k++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                m[i][j]=max(m[i][j],m[i][k]*m[k][j]);
                printf("%d %d %lf\n",i,j,m[i][j]);
            }
        }
    }
}
int main(void){
    while(~scanf("%d",&n)){
        memset(m,0.0,sizeof(m));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                scanf("%lf",&m[i][j]);
            }
        }
        floyd();
        scanf("%d",&q);
        while(q--){
            scanf("%d%d",&s,&t);
            if(m[s-1][t-1]==0){
                printf("What a pity!\n");
            }
            else{
                printf("%lf\n",m[s-1][t-1]);
            }
        }
    }
    return 0;
}
